<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../../dashboard/mixins/resize'

export default {
  mixins: [resize],
  props: {
    queryAllRateBarChartsBygroups:{
      type: Function,
      default: null
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
      
      var that = this;
      this.chart.on('click',function(params){
        
        // alert("饼图点击事件");window.open('https://www.baidu.com/s?wd=' + encodeURIComponent(params.name));
        var index = params.dataIndex;
        // console.log(that.chartData,'----------------chartData')
        
        var params ={};
        // debugger;
         //var data = that.chartData.groupsdata[index];
        // var cartype = that.chartData.typedata[index];
        // var carsystem = that.chartData.systemdata[index];
        // params.groups = data;
        // params.type = cartype;
        // params.systemId = carsystem;
        
        if(that.chartData.groupsdata == null || null !=that.chartData.systemdata){
          var cartype = that.chartData.typedata[index];
          var carsystem = that.chartData.systemdata[index];
          params.systemId = carsystem;
          params.type = cartype;
        }else{
          var data = that.chartData.groupsdata[index];
          var cartype = that.chartData.typedata[index];
          params.type = cartype;
          params.groups = data;
          
        }
        // console.log(params,'----------------params1111')
 　　　  that.queryAllRateBarChartsBygroups(params);//调用接口
      })
    },
    setOptions( {xdata,tdata}  = {}) {
      this.chart.setOption({
        color: ['#3398DB'],
        title: {
        text: tdata,
        textStyle: {
              fontSize: 15,
              color: "#00FFE3"
            },
            left: '6',
            top: "10"
    },
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b} : {c}%'
    },
    series: [
        {
            name: '占比',
            type: 'pie',
            radius: '50%',
            data: xdata,
            emphasis: {
                itemStyle: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
            }
        }
    ]
     })
    }
  }
}
</script>
